﻿MPT.addAction("myspace_menu", function(_elm) {

    var zl_jQ = $('.e_zl', _elm);
    var sub_nav_jQ = $('.e_sub_nav', _elm);
    var rw_show_jQ = $('.e_rw_show', _elm);
    var gift_jQ = $('.e_gift', _elm);
    var signin_jQ = $('.e_signin', _elm);
    var signin_dlg_jQ = $('.e_signin_dlg', _elm);

    zl_jQ.hover(function() {
        sub_nav_jQ.show();
    }, function() {
        sub_nav_jQ.hide();
    });

    sub_nav_jQ.hover(function() {
        sub_nav_jQ.show();
    }, function() {
        sub_nav_jQ.hide();
    });

    Backend.getgiftCount(null, function(_resp) {
        if (Backend.resultValidate(_resp)) {
            if (_resp.data > 0) {
                gift_jQ.show();
            }
        } else {
            Tip.show("获得礼品数量失败");
        }
    }, function() {
        Tip.show("获得礼品数量失败");
    });


    signin_jQ.one('click', function() {
        addActionSignin();
        signin_jQ.click(function() {
            if (signin_dlg_jQ.css('display') != 'none') {
                signin_dlg_jQ.hide();
            } else {
                signin_dlg_jQ.show();
            }
        });
    });


    //==============================签到对话框全部事件============================
    var addActionSignin = function() {

        var signin_box_jQ = $(MPT.getTmpl('signin_box'));
        var close_jQ = $('.e_close', signin_box_jQ);
        var cur_jQ = $('.e_cur', signin_box_jQ);
        var total_jQ = $('.e_total', signin_box_jQ);
        var my_signin_jQ = $('.e_my_signin', signin_box_jQ);
        var frd_signin_jQ = $('.e_frd_signin', signin_box_jQ);
        var pub_signin_jQ = $('.e_pub_signin', signin_box_jQ);
        var signin_panel_jQ = $('.e_signin_panel', signin_box_jQ);

        //表情面板
        var signin_emotion_jQ = $('.e_signin_emotion', signin_box_jQ);
        var emotion_list_jQ = $('.e_emotion_list', signin_box_jQ);

        //输入框面板
        var signin_input_jQ = $('.e_signin_input', signin_box_jQ);
        var back_jQ = $('.e_back', signin_box_jQ);
        var release_jQ = $('.e_release', signin_box_jQ);
        var input_jQ = $('.e_input', signin_box_jQ);
        var count_jQ = $('.e_count', signin_box_jQ);
        var face_jQ = $('.e_face', signin_box_jQ);

        //日历面板
        var signin_calendar_jQ = $('.e_signin_calendar', signin_box_jQ);
        var calendar_jQ = $('#calendar', signin_box_jQ);
        var show_jQ = $('.e_show', signin_box_jQ);
        var pre_jQ = $('.e_pre', signin_box_jQ);
        var next_jQ = $('.e_next', signin_box_jQ);
        var year_jQ = $('.e_year', signin_box_jQ);
        var month_jQ = $('.e_month', signin_box_jQ);

        var cur_year, cur_month, work_year, work_month;
        var input_load = false, calendar_load = false, frd_load = false;

        var switchTab = function(_tab_jQ) {
            my_signin_jQ.removeClass('active');
            frd_signin_jQ.removeClass('active');
            if (pub_signin_jQ) { pub_signin_jQ.removeClass('active'); }

            _tab_jQ.addClass('active');
        };

        var swtichPanel = function(_panel_jQ) {
            signin_panel_jQ.children().hide();
            _panel_jQ.show();
        }

        var switchCalendar = function(_year, _month) {
            _month = _month + 1;
            year_jQ.html(_year);
            month_jQ.html(_month);

            var records = {};
            var data = { "signMonth": _year.toString() + (_month < 10 ? "0" + _month.toString() : _month.toString()) };

            Backend.findSignSchedule(data, function(_resp) {
                if (Backend.dataValidate(_resp)) {
                    records = _resp.data;
                }

                swtichPanel(signin_calendar_jQ);

                calendar_jQ.calendarWidget({
                    month: _month,
                    year: _year,
                    records: records
                });

                calendar_jQ.find('td[class*=on]').hover(function() {
                    $(this).find('.e_show').css('display', 'inline');
                }, function() {
                    $(this).find('.e_show').css('display', 'none');
                });

                calendar_jQ.find('.e_show').hover(function() {
                    $(this).css('display', 'none');
                }, function() {
                    $(this).css('display', 'inline');
                });

            });
        }

        var displayCalendar = function() {
            calendar_load = true;
            switchTab(my_signin_jQ);
            Backend.getNowTime(null, function(_resp) {
                if (Backend.dataValidate(_resp)) {
                    cur_year = work_year = _resp.data.year;
                    cur_month = work_month = _resp.data.month;

                    jQuery.getScript(MPT.Config.sta_js_lib_http + '/jquery/external/jquery.calendar-widget.js', function() {
                        switchCalendar(cur_year, cur_month);
                    });
                }
            });

            pre_jQ.click(function() {
                work_year = work_year - ((work_month - 1) < 0 ? 1 : 0);
                work_month = (work_month - 1 + 12) % 12;
                switchCalendar(work_year, work_month);
            });

            next_jQ.click(function() {
                work_year = Math.floor((work_month + 1) / 12) + work_year;
                work_month = (work_month + 1) % 12;
                switchCalendar(work_year, work_month);
            });
        };

        //输入内容
        var displayInput = function(_i, _f) {
            input_load = true;

            swtichPanel(signin_input_jQ);

            face_jQ.attr({ 'src': MPT.Config['global_faces_path'] + '/' + _f + '.gif', "alt": _i });

            back_jQ.click(function() {
                swtichPanel(signin_emotion_jQ);
            });

            input_jQ.bind('keyup keypress', function() {
                var inputLen = input_jQ.val().length;
                count_jQ.text(inputLen);
                if (inputLen >= 40) {
                    count_jQ.css('color', 'red');
                    input_jQ.val(input_jQ.val().substring(0, 40));
                    count_jQ.text(40);
                } else {
                    count_jQ.css('color', '#033E46');
                }
            });

            release_jQ.click(function() {
                var data = { "content": input_jQ.val(), "faceFileName": _f + ".gif", "today": $('.e_test').val() };
                Backend.signIn(data, function(_resp) {
                    if (Backend.resultValidate(_resp)) {
                        Tip.show("签到成功，签到值+1");
                        if (calendar_load) {
                            switchCalendar(cur_year, cur_month);
                            my_signin_jQ.click();
                        } else {
                            displayCalendar();
                        }
                        signin_emotion_jQ.remove();
                        signin_input_jQ.remove();
                        pub_signin_jQ.remove();
                        frd_signin_jQ.next('span').remove();
                    } else {
                        Tip.show("发布签到失败");
                    }
                }, function() {
                    Tip.show("发布签到失败");
                });
            });
        };

        //选择表情
        var displayEmotion = function() {
            $.each(Util.faces, function(_i, _f) {
                var face_jQ = $('<li Other="' + _i + '" title="' + _i + '"><img alt="' + _i + '" src="' + MPT.Config['global_faces_path'] + '/' + _f + '.gif" /></li>');
                face_jQ.click(function() {
                    displayInput(_i, _f);
                });
                emotion_list_jQ.append(face_jQ);
            });
            signin_emotion_jQ.show();
        }


        signin_dlg_jQ.html(signin_box_jQ);

        my_signin_jQ.click(function() {
            if (calendar_load) {
                swtichPanel(signin_calendar_jQ);
            } else {
                displayCalendar();
            }
            switchTab(my_signin_jQ);
        });

        frd_signin_jQ.click(function() {
            if (calendar_load) {
                //swtichPanel();
            } else {
                //displayCalendar();
            }
            switchTab(frd_signin_jQ);
        });

        pub_signin_jQ.click(function() {
            if (input_load) {
                swtichPanel(signin_input_jQ);
            } else {
                swtichPanel(signin_emotion_jQ);
            }

            switchTab(pub_signin_jQ);
        });

        close_jQ.click(function() {
            signin_dlg_jQ.hide();
        });

        displayEmotion();
    };



});